package com.xkdx.guangguang.fragment.login;

import java.util.HashMap;

import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.xkdx.caipiao.R;
import com.xkdx.caipiao.util.Tost;
import com.xkdx.guangguang.ManagerActivity;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.fragment.login.UserModule.User;
import com.xkdx.guangguang.fragment.user.UserLoginPresenter;
import com.xkdx.guangguang.fragment.user.UserSharePrefence1;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;

public class RegisterFragment extends BaseFragment implements OnClickListener {
	
	private View view;
	private Button register;
	private EditText userName,password;
	private UserModule userModule;
	private UserLoginPresenter userLoginPresenter;
	private UserSharePrefence1 userSp;
	private ImageView registerBack;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		userSp = new UserSharePrefence1(getActivity(), IConstants.SP_USER_NEW);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.register, container, false);
		findView();
		setOnClick();
		return view;
	}

	@Override
	public void findView() {
		registerBack = (ImageView) view.findViewById(R.id.register_back);
		register = (Button) view.findViewById(R.id.register_btn);
		userName = (EditText) view.findViewById(R.id.register_user_name);
		password = (EditText) view.findViewById(R.id.register_password);
	}

	@Override
	public void showLoading() {
		RegisterInfoAction loginAction = new RegisterInfoAction(getActivity(), userName.getText().toString().trim(), 
				password.getText().toString().trim());
		userModule = new UserModule(getActivity());
		userLoginPresenter = new UserLoginPresenter(this);
		((ManagerActivity) getActivity()).addCurrentTask(userLoginPresenter);
		userLoginPresenter.setActitons(loginAction);
		userLoginPresenter.setModule(userModule);
		userLoginPresenter.execute();
		dataLoadDialog.setOnCancelListener(new OnCancelListener() {
		
			@Override
			public void onCancel(DialogInterface dialog) {
				if (userLoginPresenter != null) {
					userLoginPresenter.cancel(true);
				}
				getFragmentManager().popBackStack();
		
			}
		});
		dataLoadDialog.show();
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showClick(HashMap<String, AbsModule> msg) {
		dataLoadDialog.dismiss();
		if (msg.containsKey("0")) {
//			HomeModule homeModule = (HomeModule) msg.get("0");
//			single = homeModule.list;
//			nearByList = homeModule.nearInfoList;
//			setListValuse();
//			ll_discout.setVisibility(View.VISIBLE);
//			shop_redshownum.setText(nearByList.size() + "");
//			((HomeActivity) getActivity()).setHomeModule(homeModule);

		} else {
			showErrorTip(msg);
		}

	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
	}

	@Override
	public void setOnClick() {
		register.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(TextUtils.isEmpty(userName.getText().toString())){
					Tost.toast(getActivity(), "账号不能为空", null);
					//Toast.makeText(Login.this, "账号不能为空", 1).show();
					return;
				}
				if (TextUtils.isEmpty(password.getText().toString())) {
					Toast.makeText(getActivity(), "密码不能为空", 1).show();
					return;
				}
				showLoading();
			}
		});
		registerBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				getFragmentManager().popBackStack();
			}
		});
	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {
		if (dataLoadDialog != null && dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}

		if (map.containsKey("0")) {
			User  quickUser = ((UserModule) map.get("0")).user;
			if (quickUser != null && quickUser.code != null
					&& quickUser.code.equals("0000")) {
				if (userSp != null && (!"".equals(userSp.getLogintoken()))) {
//					Intent inten = new Intent(getActivity(), UserActivity.class);
//					startActivity(inten);
					getActivity().finish();
				}
			} else if(quickUser != null) {
				Toast.makeText(getActivity(), quickUser.message,
						Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(getActivity(), "注册失败，服务器异常",
						Toast.LENGTH_SHORT).show();
			}
		} else {
			showErrorTip(map);
		}

		
	}



}
